package com.hqq.behaviour.memento;

/**
 * Star
 * Created by heqianqian on 2017/6/28.
 */
public class Star {

    private int ageYears;
    private int massTons;
    private StarType starType;


    public Star(int ageYears, int massTons, StarType starType) {
        this.ageYears = ageYears;
        this.massTons = massTons;
        this.starType = starType;
    }

    public void timePassed() {
        ageYears *= 2;
        massTons *= 8;
        switch (starType) {
            case RED_GIANT:
                starType = StarType.WHITE_DWARF;
                break;
            case SUN:
                starType = StarType.RED_GIANT;
                break;
            case SUPERNOVA:
                starType = StarType.DEAD;
                break;
            case WHITE_DWARF:
                starType = StarType.SUPERNOVA;
                break;
            case DEAD:
                ageYears *= 2;
                massTons = 0;
                break;
            default:
                break;
        }
    }

    public int getAgeYears() {
        return ageYears;
    }

    public void setAgeYears(int ageYears) {
        this.ageYears = ageYears;
    }

    public int getMassTons() {
        return massTons;
    }

    public void setMassTons(int massTons) {
        this.massTons = massTons;
    }

    public StarType getStarType() {
        return starType;
    }

    public void setStarType(StarType starType) {
        this.starType = starType;
    }

    public StarMemento getStarMemento() {
        StarMementoInternal startMemento = new StarMementoInternal();
        startMemento.setAgeYears(this.ageYears);
        startMemento.setMassTons(this.massTons);
        startMemento.setStarType(this.starType);
        return startMemento;
    }

    public void setStarMemento(StarMemento starMemento) {
        StarMementoInternal startMementoInternal = (StarMementoInternal) starMemento;
        this.ageYears = startMementoInternal.getAgeYears();
        this.massTons = startMementoInternal.getMassTons();
        this.starType = startMementoInternal.getStarType();
    }

    @Override
    public String toString() {
        return String.format("%s age: %d years mass: %d tons", starType.toString(), ageYears, massTons);
    }
}
